package org.gx.tool.hptool.processer;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.gx.tool.hptool.pojo.EntitySturct;
import org.gx.tool.hptool.pojo.FieldPorperty;
import org.gx.tool.hptool.pojo.Project;

public class XLSFileDataProcesser extends DataProcesser{

	private int sheetIndex = 1;
	private int beginRowIndex ; 
	private int endRowIndex;
	private Config xlsConfig;
	private Config indexConfig;
	private String readFilePath;
	private int columnCount;
	
	public void setXlsConfig(Config config) {
		this.xlsConfig = config;
	}
	
	public void setIndexConfig(Config config) {
		this.indexConfig = config;
	}
	public XLSFileDataProcesser(String readFilePath) {
		this.readFilePath = readFilePath;
		this.xlsConfig = this.getDefaultConfig();
		this.indexConfig = this.getDefaultConfig();
	}
	
	
	public Project readProjectData() {
		String sturctName = null;
		String sturctPackagePath = null;
		EntitySturct sturct = null;
		Project project = new Project();
		
		try {
			InputStream in = new FileInputStream(this.readFilePath);
			HSSFWorkbook workbook = new HSSFWorkbook(in);
			HSSFSheet sheet = workbook.getSheetAt(this.sheetIndex);
			
			for (int rowIndex = beginRowIndex ;rowIndex < endRowIndex ; rowIndex++) {
				HSSFRow  row = sheet.getRow(rowIndex);
				if (null == row ) {
					if (null != sturct) project.addSturct(sturct);
					continue; 
				}
				
				if (rowIndex == endRowIndex) project.addSturct(sturct); 
				Cell[] cells = new Cell[columnCount];
				cells[indexConfig.fieldNameIndex]  = xlsConfig.fieldNameIndex != null ?  row.getCell(xlsConfig.fieldNameIndex) : null ;
				cells[indexConfig.fieldTypeIndex]  = xlsConfig.fieldTypeIndex != null ? row.getCell(xlsConfig.fieldTypeIndex) : null ;
				cells[indexConfig.integerLengthIndex]  = xlsConfig.integerLengthIndex != null ? row.getCell(xlsConfig.integerLengthIndex) : null;
				cells[indexConfig.decimalsLengthIndex]  = xlsConfig.decimalsLengthIndex != null ? row.getCell(xlsConfig.decimalsLengthIndex) : null;
				cells[indexConfig.descriptionIndex]  = xlsConfig.descriptionIndex != null ? row.getCell(xlsConfig.descriptionIndex) : null;
				cells[indexConfig.targetSturctIndex]  = xlsConfig.targetSturctIndex != null ?row.getCell(xlsConfig.targetSturctIndex) : null;
				cells[indexConfig.sturctPackagePathIndex] = xlsConfig.sturctPackagePathIndex != null ? row.getCell(xlsConfig.sturctPackagePathIndex) : null;
				cells[indexConfig.defaultValIndex] = xlsConfig.defaultValIndex != null ? row.getCell(xlsConfig.defaultValIndex) : null;
				cells[indexConfig.remarkIndex] =  xlsConfig.remarkIndex != null ? row.getCell(xlsConfig.remarkIndex) : null;
				cells[indexConfig.primaryIndex] = xlsConfig.primaryIndex != null ? row.getCell(xlsConfig.primaryIndex) :null;
				
				if (null == cells[indexConfig.targetSturctIndex] || null == cells[indexConfig.fieldNameIndex]) {
					continue;
				}
				
				 
				if  (sturctName == null || (sturctName!= null && !sturctName.equals(cells[indexConfig.targetSturctIndex].getStringCellValue() ))) {
					 
					sturctName = cells[indexConfig.targetSturctIndex].getStringCellValue();
					sturctPackagePath = cells[indexConfig.sturctPackagePathIndex] != null ? cells[indexConfig.sturctPackagePathIndex].getStringCellValue() : null;
					sturct = new EntitySturct(sturctName);
					sturct.setStructPackagePath(sturctPackagePath);
				}
				
				sturct.addField(this.setFieldPorperty(cells));
				project.addSturct(sturct);
				
			}
			workbook.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return project;
		 
	}
	
	/**
	 * 设置
	 * @param fieldCells
	 * @return
	 */
	private FieldPorperty setFieldPorperty(Cell[] fieldCells) {
		
		FieldPorperty field = new FieldPorperty();
		field.setField(fieldCells[indexConfig.fieldNameIndex].getStringCellValue());
		field.setFieldType(fieldCells[indexConfig.fieldTypeIndex].getStringCellValue());
		Cell intgerLengthCell = fieldCells[indexConfig.integerLengthIndex];
		if ( null != intgerLengthCell )
		field.setIntegerLength(new Double(intgerLengthCell.getNumericCellValue()).intValue());
		//field.setDecimalsLength(Integer.parseInt(fieldCells[indexConfig.decimalsLengthIndex].getNumericCellValue()+""));
		Cell descriptionCell = fieldCells[indexConfig.descriptionIndex];
		if (null != descriptionCell) 
		field.setDescription(descriptionCell.getStringCellValue());
		return field;
	}
	

	
	
	public int getSheetIndex() {
		return sheetIndex;
	}

	public void setSheetIndex(int sheetIndex) {
		this.sheetIndex = sheetIndex;
	}

	public int getBeginRowIndex() {
		return beginRowIndex;
	}

	public void setBeginRowIndex(int beginRowIndex) {
		this.beginRowIndex = beginRowIndex;
	}

	public int getEndRowIndex() {
		return endRowIndex;
	}

	public void setEndRowIndex(int endRowIndex) {
		this.endRowIndex = endRowIndex;
	}


	public int getColumnCount() {
		return columnCount;
	}

	public void setColumnCount(int columnCount) {
		this.columnCount = columnCount;
	}


	
	public Config getDefaultConfig() {
		Config defaultConfig = new Config();
		
		defaultConfig.targetSturctIndex = 1 ; //关联表索引
		defaultConfig.fieldNameIndex = 2; // 属性索引
		defaultConfig.fieldTypeIndex = 3; //属性类型索引
		defaultConfig.integerLengthIndex = 4; //整数位长度索引
		defaultConfig.decimalsLengthIndex = 5;//小数位索长度引
		defaultConfig.defaultValIndex = 6; //默认值索引
		defaultConfig.descriptionIndex = 7;//描述索引
		defaultConfig.primaryIndex = 8;//主键标识索引
		defaultConfig.remarkIndex = 9;//备注索引
		defaultConfig.sturctPackagePathIndex =10;//关联包路径索引
		
		return defaultConfig; 
	}


	public class Config {
		private Integer fieldNameIndex; // 属性索引
		private Integer fieldTypeIndex; //属性类型索引
		private Integer primaryIndex;//主键标识索引
		private Integer integerLengthIndex; //整数位长度索引
		private Integer decimalsLengthIndex;//小数位索长度引
		private Integer descriptionIndex;//描述索引
		private Integer targetSturctIndex; //关联表索引
		private Integer sturctPackagePathIndex;//关联包路径索引
		private Integer defaultValIndex; //默认值索引
		private Integer remarkIndex;//备注索引
		
		
		public Integer getFieldNameIndex() {
			return fieldNameIndex;
		}
		public void setFieldNameIndex(Integer fieldNameIndex) {
			this.fieldNameIndex = fieldNameIndex;
		}
		public Integer getFieldTypeIndex() {
			return fieldTypeIndex;
		}
		public void setFieldTypeIndex(Integer fieldTypeIndex) {
			this.fieldTypeIndex = fieldTypeIndex;
		}
		public Integer getPrimaryIndex() {
			return primaryIndex;
		}
		public void setPrimaryIndex(Integer primaryIndex) {
			this.primaryIndex = primaryIndex;
		}
		public Integer getIntegerLengthIndex() {
			return integerLengthIndex;
		}
		public void setIntegerLengthIndex(Integer integerLengthIndex) {
			this.integerLengthIndex = integerLengthIndex;
		}
		public Integer getDecimalsLengthIndex() {
			return decimalsLengthIndex;
		}
		public void setDecimalsLengthIndex(Integer decimalsLengthIndex) {
			this.decimalsLengthIndex = decimalsLengthIndex;
		}
		public Integer getDescriptionIndex() {
			return descriptionIndex;
		}
		public void setDescriptionIndex(Integer descriptionIndex) {
			this.descriptionIndex = descriptionIndex;
		}
		public Integer getTargetSturctIndex() {
			return targetSturctIndex;
		}
		public void setTargetSturctIndex(Integer targetSturctIndex) {
			this.targetSturctIndex = targetSturctIndex;
		}
		public Integer getSturctPackagePathIndex() {
			return sturctPackagePathIndex;
		}
		public void setSturctPackagePathIndex(Integer sturctPackagePathIndex) {
			this.sturctPackagePathIndex = sturctPackagePathIndex;
		}
		public Integer getDefaultValIndex() {
			return defaultValIndex;
		}
		public void setDefaultValIndex(Integer defaultValIndex) {
			this.defaultValIndex = defaultValIndex;
		}
		public Integer getRemarkIndex() {
			return remarkIndex;
		}
		public void setRemarkIndex(Integer remarkIndex) {
			this.remarkIndex = remarkIndex;
		}
		
		
		
		
		
	}
	

}
